<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        string strFileName;
        string strFileExtension;
        int intLastIndex;
        if (Request.Files.Count == 1)
        {
            try
            {
                strFileName = myFile.PostedFile.FileName;
                intLastIndex = strFileName.LastIndexOf("\\");
                if (intLastIndex > 0)
                {
                    intLastIndex += 1;
                    strFileName = strFileName.Substring(intLastIndex, (strFileName.Length - intLastIndex));
                    strFileExtension = strFileName.Substring(strFileName.Length - 4, 4);
                    if (strFileExtension == ".txt")
                    {
                        myFile.PostedFile.SaveAs(Server.MapPath(".") + "\\" + strFileName);
                        lblMsg.Text = strFileName + " Uploaded Sucessfully!";
                    }
                    else
                    {
                        lblMsg.Text = "Only Text File (.txt) can be uploaded.";
                    }
                }
                else
                {
                    lblMsg.Text = "Please Select a File!";
                }
            }
            catch (Exception exc)
            {
                lblMsg.Text = exc.Message;
            }
        }
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<script type="text/javascript">
    function SubmitForm()
    {
        // Simply, submit the form
        document.form1.submit ();
    }
</script>
    <title>Upload</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <input type="file" runat="server" id="myFile" name="myFile" style="visibility:hidden;" /> 
        <input type="button" runat="server" id="btnSubmit" name="btnSubmit" onclick="javascript:SubmitForm();" style="visibility:hidden;" /> 
        <br /><asp:Label ID="lblMsg" runat="server" ForeColor="red" Font-Size="Medium" Font-Bold="true"></asp:Label>    
    </div>
    </form>
</body>
</html>
